//
//  WPDetectionResultTopTableViewCell.m
//  health
//
//  Created by apple on 2021/11/10.
//  Copyright © 2021 zglee. All rights reserved.
//

#import "MJBloodSugarResultICell.h"
#import "TCReferenceRangeView.h"
#import "HBReferenceRangeView.h"
#import "GluReferenceRangeView.h"
#import "WenLabel.h"
#import "BloodSugarChartView.h"

@interface MJBloodSugarResultICell ()
@property (weak, nonatomic) IBOutlet UILabel *abbrLabel;

@property (weak, nonatomic) IBOutlet UIView *evaluationView;
@property (weak, nonatomic) IBOutlet UILabel *evaluationTitleLabel;
@property (weak, nonatomic) IBOutlet UILabel *evaluationValueLabel;

@property (weak, nonatomic) IBOutlet UILabel *gradeTitleLabel;
@property (weak, nonatomic) IBOutlet UILabel *gradeLabel;

@property (weak, nonatomic) IBOutlet UIView *lineView;
@property (weak, nonatomic) IBOutlet GradientRampView *gradientRampView;

@property (weak, nonatomic) IBOutlet UIView *arrowsBgView;
@property (weak, nonatomic) IBOutlet UIImageView *arrowsImageView;
@property (weak, nonatomic) IBOutlet UIImageView *gouImageView;
@property (weak, nonatomic) IBOutlet UILabel *valueLabel;
@property (weak, nonatomic) IBOutlet UILabel *unitLabel;

@property (weak, nonatomic) IBOutlet UIView *gradualArrowsView;
@property (weak, nonatomic) IBOutlet UILabel *differenceValueLabel;
@property (weak, nonatomic) IBOutlet UILabel *standardValueLabel;
@property (weak, nonatomic) IBOutlet UIImageView *differenceImageView;

//
@property (weak, nonatomic) IBOutlet UIButton *wenButton;
@property (weak, nonatomic) IBOutlet WenLabel *wenInsetsLabel;

@property (weak, nonatomic) IBOutlet UIImageView *rangeImageView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *rangeImageViewScale;

//
@property (weak, nonatomic) IBOutlet BloodSugarChartView *bloodSugarChartView;


@end

@implementation MJBloodSugarResultICell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.gradualArrowsView.hidden = true;
    self.arrowsBgView.hidden = true;
    self.gouImageView.hidden = true;

    //用此方法能让图片高度随右边的文本高度自适应，UIView设置背景图片
    UIImage *image = [UIImage imageNamed:@"bg_xian"];
    self.lineView.layer.contents = (id)image.CGImage;
    
//    self.rightImvTitleView.hidden = true;
    self.wenButton.hidden = true;
    self.wenInsetsLabel.hidden = true;

    //

}

- (void)layoutSubviews {
    [super layoutSubviews];

    [self.bloodSugarChartView.superview layoutIfNeeded];
    [self.bloodSugarChartView mj_gradientLayerWithColors:@[(id)MJUIColorFromHex(0xFFFFFF).CGColor,(id)MJUIColorFromHex(0xF3F3F3).CGColor] startPoint:CGPointMake(0, 0) endPoint:CGPointMake(0, 1)];
}

- (IBAction)wenClick:(id)sender {
    //stackView 里面的 wenButton 点击没反应，重新添加一个？？？？？
    if(!self.wenButton.isHidden){
        [self.wenInsetsLabel show];
    }
}


- (void)updateDifferenceUI:(BOOL)down {
    if(down){
        self.differenceValueLabel.textColor = MJUIColorFromHex(0x0027CB);
        self.differenceImageView.image = MJUIImageMake(@"bg_uuaxian");
    }else{
        self.differenceValueLabel.textColor = MJUIColorFromHex(0xFF0000);
        self.differenceImageView.image = MJUIImageMake(@"bg_uaxian");
    }
}

- (void)updateWenUI:(NSString *)evaluationString detectionItemType:(DetectionItemType)detectionItemType{
    if([evaluationString isEqualToString:@"Hi"] || [evaluationString isEqualToString:@"Lo"]){
        self.wenButton.hidden = false;
        BOOL isHi = false;
        if([evaluationString isEqualToString:@"Hi"]){
            isHi = true;
        }
        NSString *toast = @"";
        NSString *text1 = isHi ? MJUILocalizedString(@"For results more than") : MJUILocalizedString(@"For results less than");
        NSString *text2 = isHi ? MJUILocalizedString(@"or more than") : MJUILocalizedString(@"or less than");
       
        if(detectionItemType == DetectionItemTypeGlu){
            NSString *value1 = [isHi ? @"33.3" : @"1.10" stringByAppendingString:unitmmolL];
            NSString *value2 = [isHi ? @"600" : @"20" stringByAppendingString:unitmgdL];
            toast = [NSString stringWithFormat:@"%@ %@\n%@ %@",text1,value1,text2,value2];
        }
        
        self.wenInsetsLabel.text = toast;
        
        if(self.isFromDetection){
            [self.wenInsetsLabel show];
        }
    }
}


- (void)updateRangeImageViewWithDetectionItemType:(DetectionItemType)detectionItemType{
    
    NSString *imageName = @"";
    if(detectionItemType == DetectionItemTypeGlu){
        imageName = @[@"biao2_yin",@"biao2_fan",@"biao2_jian",@"biao2_jian",@"biao2_ru",@"biao2_id"][LanguageHelper.shared.mLanguageStyle];
    }
    self.rangeImageView.image = MJUIImageMake(imageName);
}

- (void)setBloodSugarModel:(MJBloodSugarModel *)bloodSugarModel {
    
    self.bloodSugarChartView.patientId = bloodSugarModel.patientId;
    
    self.abbrLabel.mj_localizable = bloodSugarAbbr;
    self.valueLabel.text = bloodSugarModel.bloodGlucoseString;
    self.unitLabel.text = bloodSugarModel.unit;

    self.gradientRampView.type = DetectionItemTypeGlu;
    self.gradientRampView.titleArray = @[@"Hypo",@"Normal",@"Prediabetes",@"Diabetes",@"DKA"];
    self.gradientRampView.index = bloodSugarModel.gradeValue;
    
    self.evaluationTitleLabel.mj_localizable = @"BG Diagnosis";
    self.evaluationValueLabel.mj_localizable = bloodSugarModel.evaluationString;
    self.gradeTitleLabel.mj_localizable = @"BG Prediction";
    self.gradeLabel.mj_localizable = bloodSugarModel.prediction;
    
    //
    if (bloodSugarModel.showArrowsUp) {
        self.arrowsBgView.hidden = false;
        self.arrowsImageView.image = MJUIImageMake(@"icon_shangjiantou");
        self.gradualArrowsView.hidden = false;
        
        self.differenceValueLabel.text = bloodSugarModel.differenceValue;
        self.standardValueLabel.text = bloodSugarModel.standardValue;
        
        [self updateDifferenceUI:false];

    }
    if (bloodSugarModel.showArrowsDown) {
        self.arrowsBgView.hidden = false;
        self.arrowsImageView.image = MJUIImageMake(@"icon_xiajiantou");
        self.gradualArrowsView.hidden = false;
        
        self.differenceValueLabel.text = bloodSugarModel.differenceValue;
        self.standardValueLabel.text = bloodSugarModel.standardValue;
        
        [self updateDifferenceUI:true];

    }
    if(!bloodSugarModel.showArrowsUp && !bloodSugarModel.showArrowsDown){
        self.gouImageView.hidden = false;
    }
    
    //
    if(!_bloodSugarModel){
        [self updateWenUI:bloodSugarModel.bloodGlucoseString detectionItemType:DetectionItemTypeGlu];
    }
    _bloodSugarModel = bloodSugarModel;

    [HealthHelper updateHiLoWithLabel:self.valueLabel];

    [self updateRangeImageViewWithDetectionItemType:DetectionItemTypeGlu];

}
    

@end
